var value = 0
var classification = 0
var page_number = new Array(1, 1, 1, 1, 1)


$(document).ready(function() {
    get_first(classification, 1)
})

$('.top').on('click', function() {
    $('#page_input').val('')
    value = $(this).attr('value')
    if (value == '1' || value == '2') $('#page_number')[0].style.display = 'none'
    else $('#page_number')[0].style.display = 'block'
    for (var i = 0; i < 5; i++) {
        if (i == value) {
            $('#top' + i).addClass('active')
            $('#show' + i)[0].style.display = 'block'
        } else {
            $('#top' + i).removeClass('active')
            $('#show' + i)[0].style.display = 'none'
        }
    }
    $('#current_page').html(page_number[value])
})

$('.next_page').on('click', function(e) {
    e.preventDefault()
    get_message(parseInt(page_number[value]) + 1)
})

$('.switch').on('click', function() {
    $('#page_input').val('')
    classification = $(this).attr('classification')
    rank = $(this).attr('value')
    for (var i = 0; i < 16; i++) {
        if (i == rank) {
            document.getElementsByClassName('switch')[i].parentElement.className = 'active'
        } else {
            document.getElementsByClassName('switch')[i].parentElement.className = ''
        }
    }
    get_first(classification)
    $('#current_page').html(page_number[value])
})

$('#jump_page').on('click', function(e) {
    e.preventDefault()
    get_page_number = $('#page_input').val()
    if (isNaN(get_page_number)) {
        alert('输入非法，请输入整数')
        return
    }
    if (parseInt(get_page_number) != get_page_number) {
        alert('输入非法，请输入整数')
        return
    }
    if (get_page_number == 1) {
        $('#first' + value)[0].style.display = 'block'
        $('#other' + value)[0].style.display = 'none'
        page_number[value] = 1
        $('#current_page').html('1')
        return
    }
    if (value == 1 || value == 2) {
        alert('发生未知错误')
        return
    }
    page_number[value] = get_page_number
    get_message(page_number[value])
})

function get_first(cla = 0, is_first = 0) {
    $.ajax({
        url: './init',
        type: 'POST',
        data: {
            'classification': cla,
            'is_first': is_first
        },
        dataType: 'json',
        success: function(data) {
            for (var i = 0; i < 5; i++) {
                $('#first' + i).html(data.result[i])
                $('#first' + i)[0].style.display = 'block'
                $('#other' + i)[0].style.display = 'none'
            }
            page_number = [1, 1, 1, 1, 1]
            $('#current_page').html('1')
            $('.hide').removeClass('hide')
            $('.item').addClass('show_border hoverclass')
            $('footer.blog-footer-box').addClass('footer_font')
            if (is_first) {
                var length = data.result[5].length
                for (var i = 1; i < length; i++) {
                    document.getElementsByClassName('switch')[i].setAttribute('classification', data.result[5][i].match(/\d+\b/))
                }
            }
        },
        error: function(xhr) {
            alert(xhr.status)
        }
    })
}

function get_message(page) {
    $.ajax({
        url: './spider',
        type: 'POST',
        data: {
            'classification': classification,
            'page_number': page,
            'value': value
        },
        dataType: 'json',
        success: function(data) {
            page_number[value] = page
            $('#other' + value).html(data.result)
            $('#first' + value)[0].style.display = 'none'
            $('#other' + value)[0].style.display = 'block'
            $('#current_page').html(page)
            $('.hide').removeClass('hide')
            $('.item').addClass('show_border hoverclass')
        },
        error: function(xhr) {
            alert(xhr.status)
        }
    })
}

function select_select() {
    var get_select = $('#select').val()
    $('#switch' + get_select).trigger('click')
}